package org.example.myleet.p1046;

import java.util.Arrays;

public class Solution {
    /**
     * 1 ms
     * 暴力解决
     */
    public int lastStoneWeight(int[] stones) {
        int len = stones.length;
        if (len == 0) {
            return 0;
        }
        if (len == 1) {
            return stones[0];
        }
        //排序是为了将最大的两个石块放在末尾
        Arrays.sort(stones);
        while (stones[len-1] > 0 && stones[len-2] > 0) {
            //取出最大的两个石块计算新石块结果
            int stone1 = stones[len-1];
            int stone2 = stones[len-2];
            if (stone1 != stone2) {
                //粉碎完剩新石块
                int newStone = Math.abs(stone1 - stone2);
                stones[len-1] = newStone;
            } else {
                //完全粉碎
                stones[len-1] = 0;
            }
            stones[len-2] = 0;
            //重新排序将最大的两个石块放在末尾
            Arrays.sort(stones);
        }
        return stones[len-1];
    }
}
